// pages/trainingAnswer/index.js
let question;
let num = 0;
let result = 0;
let flag = false;

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    answerStyle: 'default',
    correctAnswer: "correct",
    wrongAnswer: "wrong",
    questionTopic: "",
    answer1: "",
    answer2: "",
    answer3: "",
    answer4: "",
    c1: '#ECF2F2',
    c2: '#ECF2F2',
    c3: '#ECF2F2',
    c4: '#ECF2F2',
    userInfo: {},
    answer: {},
    personalScroe:'',
    countDownNum: '100',//倒计时初始值
  },

  /**
   * 生命周期函数--监听页面加载
   * 
   * 
   * 
   * 
   */

  
  onLoad: function(options) {
    let that = this;

    this.setData({
      userInfo: getApp().globalData.userInfo,
      personalScroe: 0
    })
   
  
    wx.request({
      url: getApp().globalData.url +'/practiceTitle.do',
      method: "GET",
      data: {
        tollgate: getApp().globalData.level,
        // tollgate: 3,
      },
      header: {
        'content-type': 'json'
      },
      success: function (res) {
        if (res.statusCode > 201){
          wx.redirectTo({
            url: '../trainingChoice/index',
          })
        }else{
          console.log(res);
          question = res.data;
          that.show();
          that.countDown();
        }
      },
      fail: function (res) {
        wx.redirectTo({
          url: '../trainingChoice/index',
        })
      }
    })
  },

  answerClick: function (event) {
    let answers = event.currentTarget.id;
    let that = this;
    if (flag) {
      flag = false;
      answers = parseInt(answers);
      result = parseInt(result);
      if (answers == result) {
        that.data.personalScroe = that.data.personalScroe + 10;
        that.judgment(result,answers,1);
        setTimeout(function(){
          that.show();
        },1000);
      } else {
        that.judgment(result, answers,0);
        setTimeout(function () {
          that.show();
        }, 1000);
      }
    }
  },

  judgment: function (result,answer,judge){
    let that = this;
    if(judge == 1){
      that.setData({
        personalScroe: that.data.personalScroe,
        ['c' + answer]: '#8DF9A4',
      })
    }else{
      that.setData({
        personalScroe: that.data.personalScroe,
        ['c' + answer]: '#F790AD',
        ['c' + result]: '#8DF9A4',
      })
    }
    
  },

  over: function(){
    let that = this;
    getApp().globalData.trainscore = that.data.personalScroe;
    question = {};
    num = 0;
    result = 0;
    flag = false;
    that.data.personalScroe = 0;
    clearInterval(that.data.timer);
    getApp().globalData.time = that.data.countDownNum;
    setTimeout(function(){
      wx.redirectTo({
        url: '../trainingResult/index',
      })
    },1000)
  },


  show: function(){
    if(num > 9){
      this.over();
    }else{
      this.setData({
      questionTopic: question[num].title,
      answer1: question[num].answer1,
      answer2: question[num].answer2,
      answer3: question[num].answer3,
      answer4: question[num].answer4,
      c1: '',
      c2: '',
      c3: '',
      c4: '',
    });
    result = question[num].correctanswer;
    num++;
    flag = true;
    }
    
  },

  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
          //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
          clearInterval(that.data.timer);
          //关闭定时器之后，可作其他处理codes go here
          that.over();
        }
      }, 1000)
    })
  },

  

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    let that = this;
    question = {};
    num = 0;
    result = 0;
    flag = false;
    that.data.personalScroe = 0;
    clearInterval(that.data.timer);
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },



});
